

#include "GN1621.h"
#include "GN1621_Parts.h"
#include "GN1621_Parts_Num.h"
#include "UI.h"
#include "../MCU_Mold/MCU_AbstractDigitron.h"
#include "APP_Main.h"


void Lcd_Probe_Icon(Char_E dis, Char_E dis1, Char_E dis2)
{
    //PC
    Cad_Dat.Cad_Com = COM_4;
    Cad_Dat.Cad_Seg = 29 - 1; //Pin29 - 1 = Seg28
    Cad_Dat.Cad_Dis = dis; 
    GN_RAM_Refresh(Cad_Dat);

    //MP1
    Cad_Dat.Cad_Com = COM_4;
    Cad_Dat.Cad_Seg = 23 - 1;
    Cad_Dat.Cad_Dis = dis1; 
    GN_RAM_Refresh(Cad_Dat);

    //MP2
    Cad_Dat.Cad_Com = COM_4;
    Cad_Dat.Cad_Seg = 17 - 1;
    Cad_Dat.Cad_Dis = dis2; 
    GN_RAM_Refresh(Cad_Dat);
}
void Lcd_Units_C(Char_E dis, Char_E dis1, Char_E dis2, Char_E dis3)
{
    uint16_t i = 0;

    Cad_Dat.Cad_Com = COM_4;
    Cad_Dat.Cad_Seg = 27 - 1;
    Cad_Dat.Cad_Dis = dis; 
    GN_RAM_Refresh(Cad_Dat);

    Cad_Dat.Cad_Com = COM_4;
    Cad_Dat.Cad_Seg = 21 - 1;
    Cad_Dat.Cad_Dis = dis1; 
    GN_RAM_Refresh(Cad_Dat);

    Cad_Dat.Cad_Com = COM_4;
    Cad_Dat.Cad_Seg = 15 - 1;
    Cad_Dat.Cad_Dis = dis2; 
    GN_RAM_Refresh(Cad_Dat);
    // Lcd_Number_Unit();

    if(dis3 == Char_ON)
    {
        for (i = 0; i < 8; i++)
        {
            Cad_Dat.Cad_Com = Temp_Unit_Icon[i][0];
            Cad_Dat.Cad_Seg = Temp_Unit_Icon[i][1];
            Cad_Dat.Cad_Dis = (((Nixie_NumEncoder[Nixie_NumEncoder_C] >> i) + Nixie_Num_Inverse) & 0x01);
            GN_RAM_Refresh(Cad_Dat); 
        }
    }
}
void Lcd_Units_F(Char_E dis, Char_E dis1, Char_E dis2, Char_E dis3)
{
    uint16_t i = 0;

    Cad_Dat.Cad_Com = COM_4;
    Cad_Dat.Cad_Seg = 25 - 1;
    Cad_Dat.Cad_Dis = dis; 
    GN_RAM_Refresh(Cad_Dat);

    Cad_Dat.Cad_Com = COM_4;
    Cad_Dat.Cad_Seg = 19 - 1;
    Cad_Dat.Cad_Dis = dis1; 
    GN_RAM_Refresh(Cad_Dat);

    Cad_Dat.Cad_Com = COM_4;
    Cad_Dat.Cad_Seg = 13 - 1;
    Cad_Dat.Cad_Dis = dis2; 
    GN_RAM_Refresh(Cad_Dat);
    // Lcd_Number_Unit();
    
    if(dis3 == Char_ON)
    {
        for (i = 0; i < 8; i++)
        {
            Cad_Dat.Cad_Com = Temp_Unit_Icon[i][0];
            Cad_Dat.Cad_Seg = Temp_Unit_Icon[i][1];
            Cad_Dat.Cad_Dis = (((Nixie_NumEncoder[Nixie_NumEncoder_F] >> i) + Nixie_Num_Inverse) & 0x01);
            GN_RAM_Refresh(Cad_Dat); 
        }
    } 
}
void Lcd_Units(Char_E dis)
{
    if(UI_System.Grill_Unit == Unit_C)
    { //C
        if(UI_System.State_Operation != UI_None) Lcd_Units_C(dis, Char_OFF, Char_OFF, dis);
        else Lcd_Units_C(dis, dis, dis, dis);
        Lcd_Units_F(Char_OFF, Char_OFF, Char_OFF, Char_OFF);
    }
    else
    { //F
        Lcd_Units_C(Char_OFF, Char_OFF, Char_OFF, Char_OFF);
        if(UI_System.State_Operation != UI_None) Lcd_Units_F(dis, Char_OFF, Char_OFF,dis);
        else Lcd_Units_F(dis, dis, dis,dis);
    }
}
void Lcd_Line(Char_E dis)
{
    Cad_Dat.Cad_Com = COM_4;
    Cad_Dat.Cad_Seg = 11 - 1;
    Cad_Dat.Cad_Dis = dis; 
    GN_RAM_Refresh(Cad_Dat);
}
void Lcd_Set(Char_E dis)
{
    Cad_Dat.Cad_Com = COM_4;
    Cad_Dat.Cad_Seg = 9 - 1;
    Cad_Dat.Cad_Dis = dis; 
    GN_RAM_Refresh(Cad_Dat);
}
void Lcd_Act(Char_E dis)
{
    Cad_Dat.Cad_Com = COM_4;
    Cad_Dat.Cad_Seg = 7 - 1;
    Cad_Dat.Cad_Dis = dis; 
    GN_RAM_Refresh(Cad_Dat);
}
void Lcd_Blue(Char_E dis)
{
    Cad_Dat.Cad_Com = COM_1;
    Cad_Dat.Cad_Seg = 31 - 1;
    Cad_Dat.Cad_Dis = dis; 
    GN_RAM_Refresh(Cad_Dat);
}
void Lcd_Wifi(Char_E dis)
{
    Cad_Dat.Cad_Com = COM_1; //S23
    Cad_Dat.Cad_Seg = 32 - 1;
    Cad_Dat.Cad_Dis = dis; 
    GN_RAM_Refresh(Cad_Dat);

    Cad_Dat.Cad_Com = COM_2; //S24
    Cad_Dat.Cad_Seg = 32 - 1;
    Cad_Dat.Cad_Dis = dis; 
    GN_RAM_Refresh(Cad_Dat);

    Cad_Dat.Cad_Com = COM_3; //S25
    Cad_Dat.Cad_Seg = 32 - 1;
    Cad_Dat.Cad_Dis = dis; 
    GN_RAM_Refresh(Cad_Dat);

    Cad_Dat.Cad_Com = COM_4; //S26
    Cad_Dat.Cad_Seg = 32 - 1;
    Cad_Dat.Cad_Dis = dis; 
    GN_RAM_Refresh(Cad_Dat);
}

void Lcd_Motor(Char_E dis)
{
    Cad_Dat.Cad_Com = COM_4;
    Cad_Dat.Cad_Seg = 31 - 1;
    Cad_Dat.Cad_Dis = dis; 
    GN_RAM_Refresh(Cad_Dat);
}
void Lcd_Ignitor(Char_E dis)
{
    Cad_Dat.Cad_Com = COM_3;
    Cad_Dat.Cad_Seg = 1 - 1;
    Cad_Dat.Cad_Dis = dis; 
    GN_RAM_Refresh(Cad_Dat);
}
void Lcd_Fan(Char_E dis)
{
    Cad_Dat.Cad_Com = COM_1;
    Cad_Dat.Cad_Seg = 1 - 1;
    Cad_Dat.Cad_Dis = dis; 
    GN_RAM_Refresh(Cad_Dat);
}
void Lcd_Motor_Err(Char_E dis)
{
    Cad_Dat.Cad_Com = COM_4;
    Cad_Dat.Cad_Seg = 1 - 1;
    Cad_Dat.Cad_Dis = dis; 
    GN_RAM_Refresh(Cad_Dat);
}
void Lcd_Ignitor_Err(Char_E dis)
{
    Cad_Dat.Cad_Com = COM_2;
    Cad_Dat.Cad_Seg = 1 - 1;
    Cad_Dat.Cad_Dis = dis; 
    GN_RAM_Refresh(Cad_Dat);
}
void Lcd_Fan_Err(Char_E dis)
{
    Cad_Dat.Cad_Com = COM_1;
    Cad_Dat.Cad_Seg = 2 - 1;
    Cad_Dat.Cad_Dis = dis; 
    GN_RAM_Refresh(Cad_Dat);
}
void Lcd_NoPellet(Char_E dis)
{
    Cad_Dat.Cad_Com = COM_2;
    Cad_Dat.Cad_Seg = 2 - 1;
    Cad_Dat.Cad_Dis = dis; 
    GN_RAM_Refresh(Cad_Dat);
}
void Lcd_Fire_Animation(int32_t param)
{
    uint8_t i;
    uint8_t x;

    const uint8_t temp_Com[10] = {COM_4,COM_3,COM_1,COM_2,COM_3, COM_4,COM_4,COM_3,COM_2,COM_1}; //Fire: S11 ~ S20
    const uint8_t temp_Pin[10] = {2 - 1,2 - 1,3 - 1,3 - 1,3 - 1, 3 - 1,4 - 1,4 - 1,4 - 1,4 - 1};

    x = param / (Oven_Setting_Max_F/10);
    if(x < 1) x = 1;
    for (i = 0; i < 10; i++)
    {
        Cad_Dat.Cad_Com = temp_Com[i];
        Cad_Dat.Cad_Seg = temp_Pin[i];
        if(i < x) Cad_Dat.Cad_Dis = Char_ON;
        else Cad_Dat.Cad_Dis = Char_OFF;
        if(param <= 0) Cad_Dat.Cad_Dis = Char_OFF;
        GN_RAM_Refresh(Cad_Dat);
    }
}

void Lcd_Parts_OFF(void)
{
    Lcd_Probe_Icon(Char_OFF, Char_OFF, Char_OFF);
    Lcd_Units(Char_OFF); 
    // Lcd_Units_F(Char_OFF, Char_OFF, Char_OFF);
    // Lcd_Units_C(Char_OFF, Char_OFF, Char_OFF);

    Lcd_Line(Char_OFF);
    Lcd_Set(Char_OFF);
    Lcd_Act(Char_OFF);
    Lcd_Wifi(Char_OFF);
    Lcd_Blue(Char_OFF);
    Lcd_Motor(Char_OFF);
    Lcd_Fan(Char_OFF);
    Lcd_Ignitor(Char_OFF);
    Lcd_Motor_Err(Char_OFF);
    Lcd_Ignitor_Err(Char_OFF);
    Lcd_Fan_Err(Char_OFF);
    Lcd_NoPellet(Char_OFF);
    Lcd_Fire_Animation(0);

    Lcd_NumShow_OFF();
}

void Lcd_Parts_Init(void)
{
    Lcd_Probe_Icon(Char_ON, Char_ON, Char_ON);
    Lcd_Units(Char_ON); 
    Lcd_Line(Char_ON);
    Lcd_Set(Char_ON);
    Lcd_Act(Char_OFF);
    Lcd_Wifi(Char_ON);
    Lcd_Blue(Char_ON);
    Lcd_Motor(Char_OFF);
    Lcd_Fan(Char_OFF);
    Lcd_Ignitor(Char_OFF);

    Lcd_Motor_Err(Char_OFF);
    Lcd_Ignitor_Err(Char_OFF);
    Lcd_Fan_Err(Char_OFF);
    Lcd_NoPellet(Char_OFF);

    RTD[2].status = Pt_Open; //Probe1采集序号
    RTD[3].status = Pt_Open; //Probe2采集序号
    RTD[4].status = Pt_Open; //Probe3采集序号
    UI_System.ProbeA.Actual = 0; 
    UI_System.ProbeB.Actual = 0; 
    UI_System.ProbeC.Actual = 0; 
    Lcd_NumShow(UI_RTD, UI_System.RTD.Actual);
    Lcd_NumShow(UI_ProbeA,UI_System.ProbeA.Actual);
    Lcd_NumShow(UI_ProbeB,UI_System.ProbeB.Actual);
    Lcd_NumShow(UI_ProbeC,UI_System.ProbeC.Actual);
}

void Lcd_Parts_Main(void)
{
    if((UI_System.Grill_state != State_Grill_Misfire) && (UI_System.Grill_state != State_Grill_MisfireCountdown))
    {
        if(UI_System.ICO_WIFI == UI_Shine)
        {
            if(UI_System.Shine_WIFI == Shine_ON) Lcd_Wifi(Char_ON);
            else Lcd_Wifi(Char_OFF);
        }
        else if(UI_System.ICO_WIFI == UI_OFF) Lcd_Wifi(Char_OFF);
        else Lcd_Wifi(Char_ON);
        
        if(UI_System.State_Operation != UI_None) Lcd_Probe_Icon(Char_ON, Char_OFF, Char_OFF);
        else
        {
            Lcd_Probe_Icon(Char_ON, Char_ON, Char_ON);
            Lcd_NumShow(UI_ProbeA,UI_System.ProbeA.Actual);
        }
        Lcd_NumShow(UI_ProbeB,UI_System.ProbeB.Actual);
        Lcd_NumShow(UI_ProbeC,UI_System.ProbeC.Actual); 
    }

    if(UI_System.Grill_state == State_Grill_MisfireCountdown)
    {
        Lcd_Parts_OFF();
        if(ExhaustFanClock == 0) UI_System.Grill_state = State_Grill_Misfire;
    }
    else if(UI_System.Grill_state == State_Grill_Misfire)
    {
        GN1621_OFF();
    }
    else if(UI_System.Grill_state == State_Grill_SetTemp)
    {
        Lcd_NumShow_Shine(UI_System.RTD.Settings_Modifiing);
        Lcd_Units(Char_ON);
    }
    else
    {
        if(UI_System.State_Operation == UI_RTDSettings)
        {
            Lcd_NumShow_Shine(UI_System.RTD.Settings_Modifiing);
            Lcd_NumShow(UI_ProbeASettings,UI_System.ProbeA.Settings_Modifiing);
            Lcd_Fire_Animation(UI_System.RTD.Settings_Modifiing);
        }
        else if(UI_System.State_Operation == UI_ProbeASettings)
        {
            Lcd_NumShow(UI_RTDSettings,UI_System.RTD.Settings_Modifiing);
            Lcd_NumShow_Shine(UI_System.ProbeA.Settings_Modifiing);
        }
        else
        {
            Lcd_NumShow(UI_RTD,UI_System.RTD.Actual);
            Lcd_Fire_Animation(UI_System.RTD.Actual);
        }
        // if ((RTD[ADC_Oven].status == Pt_Open) || (RTD[ADC_Oven].status == Pt_Short))
        // {
        //     if(UI_System.Shine_AugerErr == Shine_ON)
        //     {
        //         Lcd_Motor(Char_ON);
        //         Lcd_Motor_Err(Char_ON);
        //     }
        //     else
        //     {
        //         Lcd_Motor(Char_OFF);
        //         Lcd_Motor_Err(Char_OFF);
        //     }
        // }
        // else Lcd_Motor(Char_ON); 

        if(UI_System.Shine_AugerErr == Shine_ON)
		{
			Lcd_Motor(Char_ON);
			Lcd_Motor_Err(Char_ON);
		}
		else
		{
			Lcd_Motor(Char_OFF);
			Lcd_Motor_Err(Char_OFF);
		}

		if(UI_System.Shine_FanErr == Shine_ON)
		{
			Lcd_Fan(Char_ON);
			Lcd_Fan_Err(Char_ON);
		}
		else
		{
			Lcd_Fan(Char_OFF);
			Lcd_Fan_Err(Char_OFF);
		}

		if(UI_System.Shine_IgnitorErr == Shine_ON)
		{
			Lcd_Ignitor(Char_ON);
			Lcd_Ignitor_Err(Char_ON);
		}
		else
		{
			Lcd_Ignitor(Char_OFF);
			Lcd_Ignitor_Err(Char_OFF);
		}

        Lcd_Units(Char_ON);
    }
}

//Tick 10ms
void Lcd_Parts_Clock(void)
{
	static uint16_t temp_1s = 0;
	temp_1s++;
    if(temp_1s >= 100)
    { //tick 1s
        temp_1s -= 100;

        Key_Clock();

        if(UI_System.Timer1.Actual > 0)
        {
            UI_System.Timer1.Actual--;
            if(UI_System.Timer1.Actual == 0)
            {
                UI_System.Alarm_Timer = Alarm_Timer1; //Timer Alarm Enable
                UI_System.Timer1.Settings = 0;
                // UI_System.Timer1.Alart_State = Alart_Action;
            }
        }
        
        // MemSize("Main");
    }
}
